#!/usr/bin/perl
#
# Change the first log record from UPDATE to NEW for the people
# who got this wrong (Feb 2001-May 2002, 207368 to 276219, approx)
#
use Counter;
use Getopt::Long;
use NoteHandler;

our $count = 10;
our $table = 'persons';
our $DEBUG = 0;
our $write = 1;
our $first = 207368;
our $last = 276219;

GetOptions("count=i" => \$count,
	   "table=s" => \$table,
	   "debug" => \$DEBUG,
	   "write!" => \$write,
	   "first=i" => \$first,
	   "last=i" => \$last,
	   );
# get a random filehandle for sql ops
my $stats = Counter::openfile("statistics");

my $n = new NoteHandler;
my $donecount = 0;
warn "Changing $count records from $first to $last\n";
reclist: for my $key ($first..$last) {
    $n->start($key);
    # select the first operation - this should always be a NEW
    my @changelist = $stats->selectany("* from f_log where tablename = ? and reckey = ? order by uniqueid limit 1",
				       $table, $key . ''); # enforce key=text
    $change = $changelist[0];
    if (!$change) {
	$n->note("No records");
	$n->done("No records");
    } elsif ($change->{operation} ne "new") {
	$DEBUG && $n->note($change->{operation}, ":", $change->{date});
	if ($write) {
	    my ($count, $sth) =
		$stats->dosql("update f_log set operation = ? where uniqueid = ?",
			      "new", $change->{uniqueid});
	    if ($count != 1) {
		die "Error in update\n";
	    }
	}
	$n->done("Changing from " . $change->{operation} . " to new");
    } else {
	# no change needed - already 'new'
	$n->done("Already OK");
    }
    ++ $donecount;
    last if $donecount >= $count;
}
$n->report();
